home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / networking / pgpuam / sources / machdepnetmacros.h < prev    next >
Encoding:
Text File  |  2000-06-23  |  10.2 KB  |  305 lines

  1. // 
  2. // Apple Macintosh Developer Technical Support
  3. // Written by:  Vinnie Moscaritolo
  4. //
  5. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  6. //
  7. // You may incorporate this sample code into your applications without
  8. // restriction, though the sample code has been provided "AS IS" and the
  9. // responsibility for its operation is 100% yours.  However, what you are
  10. // not permitted to do is to redistribute the source as "DSC Sample Code"
  11. // after having made changes. If you're going to re-distribute the source,
  12. // we require that you make it clear in the source that the code was
  13. // descended from Apple Sample Code, but that you've made changes.
  14. // 
  15. //
  16. // file :machdepnetmacros.h
  17. // This module contains the machine dependent network definition
  18. //
  19.  
  20. // If this is Microsoft Visual C++, disable warnings about unknown pragmas 
  21. // such as #pragma mark
  22.  
  23. #ifdef _MSC_VER
  24. #pragma warning(disable:4068)
  25. #endif
  26.  
  27. #include <ConditionalMacros.h>
  28.  
  29.  /* TARGET_RT_LITTLE_ENDIAN controls whether messages are byte-swapped. 
  30.  The choices are TARGET_RT_LITTLE_ENDIAN(default) and TARGET_RT_BIG_ENDIAN. */
  31.  
  32.  
  33. /* ALIGN controls whether move operations must be aligned on natural boundaries.
  34.  * If ALIGN == 1, all move instructions must have aligned operands.
  35.  * If ALIGN == 0 (default), hardware does not require data alignment. */
  36.  
  37. #ifndef ALIGN
  38. #if    TARGET_OS_MAC
  39. #include <string.h>
  40. #define ALIGN                    1
  41. #else
  42. #define ALIGN                    0
  43. #endif
  44. #endif
  45.  
  46. // ---------------------------------------------------------------------------
  47. // Primitive Datatypes *
  48. // ---------------------------------------------------------------------------
  49.  
  50. typedef unsigned char            UByte1;                 /* unsigned 8 bit quantity */
  51. typedef unsigned short            UByte2;                 /* unsigned 16 bit quantity */
  52. typedef unsigned long            UByte4;                 /* unsigned 32 bit quantity */
  53. typedef unsigned long long         UByte8;                 /* unsigned 64 bit quantity */
  54. typedef void *                    Opaque;                 /* pointer to arbitrary block */
  55.  
  56. typedef    UByte4                        NetworkTime;
  57.  
  58.  
  59. /* MOVE_NET macros */
  60.  
  61. // Move on byte to/from a message buffer
  62.  
  63. #ifdef TARGET_OS_MAC
  64. #define MOVE_TO_NET_1(wireMsgP, v)         { *((UByte1*)wireMsgP)++ =  (v) & 0xff; }
  65. #else
  66. #define MOVE_TO_NET_1(wireMsgP, v)         { *((UByte1*)wireMsgP)++ = (UByte1)(v);}
  67. #endif
  68. #define MOVE_FROM_NET_1(wireMsgP, v)        {(v) = *((UByte1*)wireMsgP)++;}
  69.  
  70. #define SKIP_TO_NET_1(wireMsgP)        { ((UByte1*)wireMsgP)+=1;}
  71. #define SKIP_TO_NET_2(wireMsgP)        { ((UByte1*)wireMsgP)+=2;}
  72. #define SKIP_TO_NET_4(wireMsgP)        { ((UByte1*)wireMsgP)+=4;}
  73. #define SKIP_TO_NET_8(wireMsgP)        { ((UByte1*)wireMsgP)+=8;}
  74.  
  75. #define SKIP_FROM_NET_1(wireMsgP)        { ((UByte1*)wireMsgP)+=1;}
  76. #define SKIP_FROM_NET_2(wireMsgP)        { ((UByte1*)wireMsgP)+=2;}
  77. #define SKIP_FROM_NET_4(wireMsgP)        { ((UByte1*)wireMsgP)+=4;}
  78. #define SKIP_FROM_NET_8(wireMsgP)        { ((UByte1*)wireMsgP)+=8;}
  79.  
  80. #define SKIP_FROM_NET(wireMsgP, length)    { ((UByte1*)wireMsgP)+=length;}
  81. #define SKIP_TO_NET    (wireMsgP, length)    { ((UByte1*)wireMsgP)+=length;}
  82.  
  83.  
  84. // Move a block to/from a message buffer
  85.  
  86. #define COPY_FROM_TO(from, to, length)        memmove((to), (from), (size_t)(length));
  87.  
  88. #define MEM_ZERO(memP, size)                    {UByte1 *_p =  (UByte1*) memP;  size_t _len = (size_t)size;  while(_len--)*_p++ = 0;  }
  89.  
  90. #define MOVE_FROM_NET_BLOCK(wireMsgP, v, len)        { COPY_FROM_TO((UByte1*)wireMsgP, (UByte1*)v , len); ((UByte1 *)wireMsgP)+=len;}
  91. #define MOVE_TO_NET_BLOCK(wireMsgP, v, len)            { COPY_FROM_TO((UByte1*)v, (UByte1*)wireMsgP, len);  ((UByte1 *)wireMsgP)+=len;}
  92.  
  93.  
  94.  
  95. // move to net in order
  96.  
  97. #define MOVE_TO_NET_NUMERIC_2(wireMsgP, v)                        \
  98.     {                                                            \
  99.     MOVE_TO_NET_1 (wireMsgP, (v)>>8 )                            \
  100.     MOVE_TO_NET_1 (wireMsgP, (v)>>0 )                            \
  101.     }
  102.  
  103. #define MOVE_TO_NET_NUMERIC_4(wireMsgP, v)                        \
  104.     {                                                            \
  105.     MOVE_TO_NET_1 (wireMsgP, (v)>>24)                            \
  106.     MOVE_TO_NET_1 (wireMsgP, (v)>>16)                            \
  107.     MOVE_TO_NET_1 (wireMsgP, (v)>>8 )                            \
  108.     MOVE_TO_NET_1 (wireMsgP, (v)>>0 )                            \
  109.     }
  110.  
  111. #define MOVE_TO_NET_NUMERIC_8(wireMsgP, v)                        \
  112.     {                                                            \
  113.     MOVE_TO_NET_1 (wireMsgP,  (v)>>56)                            \
  114.     MOVE_TO_NET_1 (wireMsgP,  (v)>>48)                            \
  115.     MOVE_TO_NET_1 (wireMsgP,  (v)>>40)                            \
  116.     MOVE_TO_NET_1 (wireMsgP,  (v)>>32)                            \
  117.     MOVE_TO_NET_1 (wireMsgP,  (v)>>24)                            \
  118.     MOVE_TO_NET_1 (wireMsgP,  (v)>>16)                            \
  119.     MOVE_TO_NET_1 (wireMsgP,  (v)>>8 )                            \
  120.     MOVE_TO_NET_1 (wireMsgP,  (v)>>0 )                            \
  121.     }
  122.  
  123. #define MOVE_FROM_NET_NUMERIC_2(wireMsgP, v)                    \
  124.     {                                                                        \
  125.     (v) =  (*((UByte1*)wireMsgP)++ << 8 )                        \
  126.         +  (*((UByte1*)wireMsgP)++ << 0 );                         \
  127.     }
  128.  
  129. #define MOVE_FROM_NET_NUMERIC_4(wireMsgP, v)                    \
  130.     {                                                                        \
  131.         UByte4    _uh, _lh;                                            \
  132.          MOVE_FROM_NET_NUMERIC_2(wireMsgP,  _uh)                  \
  133.         MOVE_FROM_NET_NUMERIC_2(wireMsgP , _lh)                  \
  134.         (v) = (_uh << 16) + (0xFFFF & _lh)     ;                    \
  135.      }
  136.  
  137. #define MOVE_FROM_NET_NUMERIC_8(wireMsgP, v)                    \
  138.     {                                                                        \
  139.         UByte8    _uh, _lh;                                            \
  140.          MOVE_FROM_NET_NUMERIC_4(wireMsgP,  _uh)                  \
  141.         MOVE_FROM_NET_NUMERIC_4(wireMsgP , _lh)                  \
  142.         (v) = (_uh << 32) + (0xFFFFFFFF & _lh) ;                \
  143.      }
  144.      
  145.  
  146. // move to net swapped order
  147.  
  148. #define MOVE_TO_NET_REVERSED_2(wireMsgP, v)                         \
  149.     {                                                            \
  150.     MOVE_TO_NET_1 (wireMsgP,  (v)>>0 )                            \
  151.     MOVE_TO_NET_1 (wireMsgP,  (v)>>8 )                            \
  152.     }
  153.  
  154. #define MOVE_TO_NET_REVERSED_4(wireMsgP,v)                         \
  155.     {                                                            \
  156.     MOVE_TO_NET_1 (wireMsgP, (v)>>0 )                            \
  157.     MOVE_TO_NET_1 (wireMsgP, (v)>>8 )                            \
  158.     MOVE_TO_NET_1 (wireMsgP, (v)>>16)                            \
  159.     MOVE_TO_NET_1 (wireMsgP, (v)>>24)                            \
  160.     }
  161.  
  162. #define MOVE_TO_NET_REVERSED_8(wireMsgP, v)                         \
  163.     {                                                            \
  164.     MOVE_TO_NET_1 (wireMsgP,  (v)>>0 )                            \
  165.     MOVE_TO_NET_1 (wireMsgP,  (v)>>8 )                            \
  166.     MOVE_TO_NET_1 (wireMsgP,  (v)>>16)                            \
  167.     MOVE_TO_NET_1 (wireMsgP,  (v)>>24)                            \
  168.     MOVE_TO_NET_1 (wireMsgP,  (v)>>32)                            \
  169.     MOVE_TO_NET_1 (wireMsgP,  (v)>>40)                            \
  170.     MOVE_TO_NET_1 (wireMsgP,  (v)>>48)                            \
  171.     MOVE_TO_NET_1 (wireMsgP,  (v)>>56)                            \
  172.     }
  173.  
  174.  
  175.  
  176.  
  177. #define MOVE_FROM_NET_REVERSED_2(wireMsgP, v)                    \
  178.     {                                                            \
  179.     (v) = (*((UByte1*)wireMsgP)++ << 0 )                        \
  180.         + (*((UByte1*)wireMsgP)++ << 8 );                         \
  181.     }
  182.  
  183.  
  184. #define MOVE_FROM_NET_REVERSED_4(wireMsgP, v)                    \
  185.     {                                                                        \
  186.         UByte4    _uh, _lh;                                            \
  187.          MOVE_FROM_NET_REVERSED_2(wireMsgP,  _lh)                  \
  188.         MOVE_FROM_NET_REVERSED_2(wireMsgP , _uh)                  \
  189.         (v) = (_uh << 16) + (0xFFFF & _lh)     ;                    \
  190.      }
  191.  
  192. #define MOVE_FROM_NET_REVERSED_8(wireMsgP, v)                    \
  193.     {                                                                        \
  194.         UByte8    _uh, _lh;                                            \
  195.          MOVE_FROM_NET_REVERSED_2(wireMsgP,  -lh)                  \
  196.         MOVE_FROM_NET_REVERSED_2(wireMsgP , _uh)                  \
  197.         (v) = (_uh << 32) + (0xFFFFFFFF & _lh) ;                \
  198.      }
  199.  
  200.  
  201. // move macros for machines that require move operations be aligned.
  202.  
  203. #if ALIGN
  204. #if  TARGET_RT_BIG_ENDIAN 
  205.  
  206. #define MOVE_TO_NET_2(wireMsgP, v)         MOVE_TO_NET_NUMERIC_2 (wireMsgP, v)
  207. #define MOVE_TO_NET_4(wireMsgP, v)         MOVE_TO_NET_NUMERIC_4 (wireMsgP, v)
  208. #define MOVE_TO_NET_8(wireMsgP, v)         MOVE_TO_NET_NUMERIC_8 (wireMsgP, v)
  209. #define MOVE_FROM_NET_2(wireMsgP, v)    MOVE_FROM_NET_NUMERIC_2 (wireMsgP, v)
  210. #define MOVE_FROM_NET_4(wireMsgP, v)    MOVE_FROM_NET_NUMERIC_4 (wireMsgP, v)
  211. #define MOVE_FROM_NET_8(wireMsgP, v)    MOVE_FROM_NET_NUMERIC_8 (wireMsgP, v)
  212.  
  213. #else 
  214.  
  215. #define MOVE_TO_NET_2(wireMsgP,  v)         MOVE_TO_NET_REVERSED_2 (wireMsgP, v)
  216. #define MOVE_TO_NET_4(wireMsgP,  v)         MOVE_TO_NET_REVERSED_4 (wireMsgP, v)
  217. #define MOVE_TO_NET_8(wireMsgP,  v)         MOVE_TO_NET_REVERSED_8 (wireMsgP, v)
  218. #define MOVE_FROM_NET_2(wireMsgP, v)        MOVE_FROM_NET_REVERSED_2 (wireMsgP, v)
  219. #define MOVE_FROM_NET_4(wireMsgP, v)        MOVE_FROM_NET_REVERSED_4 (wireMsgP, v)
  220. #define MOVE_FROM_NET_8(wireMsgP, v)        MOVE_FROM_NET_REVERSED_8 (wireMsgP, v)
  221.  
  222. #endif  
  223.  
  224. #else /* ALIGN */
  225.  
  226. #define MOVE_TO_NET_2(wireMsgP, v)             { *((UByte2*)wireMsgP)++ = (UByte2)(v);}
  227. #define MOVE_TO_NET_4(wireMsgP, v)             { *((UByte4*)wireMsgP)++ = (UByte4)(v);}
  228. #define MOVE_TO_NET_4(wireMsgP, v)             { *((UByte8*)wireMsgP)++ = (UByte8)(v);}
  229. #define MOVE_FROM_NET_2(wireMsgP, v)        {(v) =  *((UByte2*)wireMsgP)++;}
  230. #define MOVE_FROM_NET_4(wireMsgP, v)        {(v) =  *((UByte4*)wireMsgP)++;}
  231. #define MOVE_FROM_NET_8(wireMsgP, v)        {(v) =  *((UByte8*)wireMsgP)++;}
  232.  
  233. #endif /* ALIGN */
  234.  
  235. // move macros for big/little endian..
  236.  
  237. #if TARGET_RT_BIG_ENDIAN
  238.  
  239. #define SWAP_TO_NET_2(wireMsgP, v)             MOVE_TO_NET_2 (wireMsgP, v)
  240. #define SWAP_TO_NET_4(wireMsgP, v)             MOVE_TO_NET_4 (wireMsgP, v)
  241. #define SWAP_TO_NET_8(wireMsgP, v)             MOVE_TO_NET_8 (wireMsgP, v)
  242. #define SWAP_FROM_NET_2(wireMsgP, v)        MOVE_FROM_NET_2 (wireMsgP, v)
  243. #define SWAP_FROM_NET_4(wireMsgP, v)        MOVE_FROM_NET_4 (wireMsgP, v)
  244. #define SWAP_FROM_NET_8(wireMsgP, v)        MOVE_FROM_NET_8 (wireMsgP, v)
  245.  
  246. #else  
  247.  
  248. #define SWAP_TO_NET_2(wireMsgP,  v)         MOVE_TO_NET_NUMERIC_2 (wireMsgP,  v)
  249. #define SWAP_TO_NET_4(wireMsgP,  v)         MOVE_TO_NET_NUMERIC_4 (wireMsgP,  v)
  250. #define SWAP_TO_NET_8(wireMsgP,  v)         MOVE_TO_NET_NUMERIC_8 (wireMsgP,  v)
  251.  
  252. #define SWAP_FROM_NET_2(wireMsgP,  v)        MOVE_FROM_NET_NUMERIC_2 (wireMsgP,  v)
  253. #define SWAP_FROM_NET_4(wireMsgP,  v)        MOVE_FROM_NET_NUMERIC_4 (wireMsgP,  v)
  254. #define SWAP_FROM_NET_8(wireMsgP,  v)        MOVE_FROM_NET_NUMERIC_8 (wireMsgP,  v)
  255.  
  256. #endif  
  257.  
  258. // ---------------------------------------------------------------------------
  259. #pragma mark  -- Linked List Operators --
  260. // ---------------------------------------------------------------------------
  261.  
  262. #define INSERT_LIST_HEAD(h, unused_TYP, eP, f)    \
  263.     {                                                                                            \
  264.     (eP)->f = (h);                                                                \
  265.     (h) = (eP);                                                                     \
  266.     }
  267.  
  268. #define IF_REMOVE_LIST_HEAD(h, unused_TYP, eP, f)        \
  269.     if ((eP) = (h)) (h) = (eP)->f;
  270.  
  271. #define INSERT_LIST_TAIL(h, TYP, eP, f)                 \
  272.     {                                                                                            \
  273.     TYP **ePP = (&(h));                                                     \
  274.                                                                                                 \
  275.     while (1) {                                                                     \
  276.         if (!(*ePP)) break;                                                 \
  277.         ePP = (&(*ePP)->f);                                                 \
  278.     }                                                                                            \
  279.                                                                                                 \
  280.     (*ePP) = (eP);                                                                \
  281.     (eP)->f = 0;                                                                    \
  282.     }
  283.  
  284. #define REMOVE_LIST_ENTRY(h, TYP, eP, f)            \
  285.     {                                                                                        \
  286.     TYP **ePP, **nPP = (&(h));                                    \
  287.                                                                                             \
  288.     while (1) {                                                                 \
  289.         ePP = nPP;                                                                \
  290.         nPP = (&(*nPP)->f);                                             \
  291.         if ((*ePP) == (eP)) break;                                \
  292.     }                                                                                        \
  293.                                                                                             \
  294.     (*ePP) = (eP)->f;                                                        \
  295.     }
  296.  
  297. // ---------------------------------------------------------------------------
  298. #pragma mark  -- Set traversal operations  --
  299. // ---------------------------------------------------------------------------
  300.  
  301. #define FOR_EACH(h, eP, nextEP, f)                for (nextEP = (h); (eP = nextEP) && (nextEP = eP->f, 1); )
  302.  
  303.  
  304.  
  305.